/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cookor.poi.modules.weixin.service;

import java.util.List;

import com.cookor.poi.modules.sys.entity.Office;
import com.cookor.poi.modules.weixin.account.ThirdPlatform;
import com.xiaoleilu.hutool.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.service.CrudService;
import com.cookor.poi.modules.weixin.entity.TWeixinConfig;
import com.cookor.poi.modules.weixin.dao.TWeixinConfigDao;

/**
 * 微信tokenService
 * @author poi
 * @version 2021-03-16
 */
@Service
@Transactional(readOnly = true)
public class TWeixinConfigService extends CrudService<TWeixinConfigDao, TWeixinConfig> {

	public TWeixinConfig get(String id) {
		return super.get(id);
	}

	public List<TWeixinConfig> findList(TWeixinConfig tWeixinConfig) {
		return super.findList(tWeixinConfig);
	}

	public Page<TWeixinConfig> findPage(Page<TWeixinConfig> page, TWeixinConfig tWeixinConfig) {
		return super.findPage(page, tWeixinConfig);
	}

	@Transactional(readOnly = false)
	public void updateAuthRefreshToken(String appid,String authRefreshToken){
		dao.updateAuthRefreshToken(appid,authRefreshToken);
	}


	@Transactional(readOnly = false)
	public void save(TWeixinConfig tWeixinConfig) {
		super.save(tWeixinConfig);
	}
	@Transactional(readOnly = false)
	public void mySave(TWeixinConfig tWeixinConfig)throws Exception {
		if(tWeixinConfig.getTenantId() != null){
			TWeixinConfig old = findByTenantId(tWeixinConfig.getTenantId());
			if(!old.getId().equals(tWeixinConfig.getId())){
				throw new Exception("机构不得修改");
			}
		}
		super.save(tWeixinConfig);
	}

	@Transactional(readOnly = false)
	public void delete(TWeixinConfig tWeixinConfig) {
		super.delete(tWeixinConfig);
	}

	public TWeixinConfig findByTenantId(Integer tenantId){
		return  dao.findByTenantId(tenantId);
	}

	public TWeixinConfig findByAppid(String appid){
		TWeixinConfig wc = new TWeixinConfig();
		wc.setAppid(appid);
		List<TWeixinConfig> list = findList(wc);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Transactional(readOnly = false)
	public TWeixinConfig save(String officeId,String authorizer_refresh_token, JSONObject authorizer_info){
		JSONObject authorization_info = authorizer_info.getJSONObject("authorization_info");
		String appid = authorization_info.getStr("authorizer_appid");
		logger.info("appid:"+appid);
		TWeixinConfig wc = findByAppid(appid);
		if (wc==null){
			wc = new TWeixinConfig();
		}
		JSONObject accountInfo = authorizer_info.getJSONObject("authorizer_info");
		String func_info = authorization_info.getStr("func_info");
		wc.setAppid(appid);
		wc.setName(accountInfo.getStr("nick_name"));
		wc.setHeadImg(accountInfo.getStr("head_img"));
		JSONObject service_type_info = accountInfo.getJSONObject("service_type_info");
		if(service_type_info!=null){
			wc.setServiceTypeInfo(Integer.parseInt(service_type_info.getStr("id")));
		}
		JSONObject verify_type_info = accountInfo.getJSONObject("verify_type_info");
		if(verify_type_info!=null){
			wc.setVerifyTypeInfo(Integer.parseInt(verify_type_info.getStr("id")));
		}
		Office company = new Office();
		company.setId(officeId);
		wc.setAuthType(1);
		wc.setType(3);
		wc.setCompany(company);
		wc.setToken(ThirdPlatform.TOKEN);
		wc.setEncodingaeskey(ThirdPlatform.COMPONENTAPPSECRET);
		wc.setAlias(accountInfo.getStr("alias"));
		wc.setOriginal(accountInfo.getStr("user_name"));
		wc.setBusinessInfo(accountInfo.getStr("business_info"));
		wc.setFuncInfo(func_info);
		wc.setQrcodeUrl(accountInfo.getStr("qrcode_url"));
		wc.setAuthRefreshToken(authorizer_refresh_token);
		save(wc);
		return wc;
	}

}
